<template>
	<view class="rounded-10 bg-white" style="width: 650rpx;">
		<view class="u-p-y-30 u-p-x-45 ">
			<view class="flex flex-column">
				<slot></slot>
				<!-- <text class="u-font-30 font-weight-bold">当前价格：¥ {{currentInfo.price}}</text>
				<text class="u-font-30 font-weight-bold" v-if="currentInfo.type == 'seller'">买家价格：¥ {{currentInfo.buyer_prics}}</text> -->
				<view class="u-p-x-20 u-p-y-10 u-m-y-30 border rounded-15">
					<input v-model="dickerPrice" @input="inputPriceChange" type="digit" placeholder="请输入您要报给买家的价格" placeholder-style="fontSize: 24rpx"/>
				</view>
				<text class="text-danger u-font-24">注：卖家同意之后，可按您输入的价格购买此商品</text>
			</view>
		</view>
		<view class="flex align-center justify-evenly border-top" style="height: 100rpx;">
			<view class="flex-1 flex align-center justify-center border-right" style="height: 100rpx;" @click="$emit('cancle')">
				<text class="u-font-30" >取消</text>
			</view>
			<view class="flex-1 flex align-center justify-center" style="height: 100rpx;" @click="sure">
				<text class="u-font-30" >确定</text>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		name: "goods-dicker-popup",
		data() {
			return {
				dickerPrice: ''
			}
		},
		methods: {
			sure() {
				if(!this.dickerPrice) return uni.showToast({
					title: '请输入正确的价格',
					icon:'none'
				})
				uni.hideKeyboard()
				this.$emit('sure', this.dickerPrice)
			},
			inputPriceChange(val) {
				//重新赋值给input
				this.$nextTick(() => {
					this.dickerPrice = this.formartInputValue(val.detail.value)
				})
			},
			// 格式化input输入框 数据
			formartInputValue(val, num) {
				if(val=='') return
				// console.log('656565655655', val, num)
				// 先把非数字的都替换掉，除了数字和.
				val = val.replace(/[^\d.]/g, "");
				// 保证只有出现一个.而没有多个. 
				val = val.replace(/\.{2,}/g, ".");
				// 必须保证第一个为数字而不是. 
				val = val.replace(/^\./g, "");
				// 保证.只出现一次，而不能出现两次以上 
				val = val.replace(".", "$#$").replace(/\./g, "").replace("$#$", ".");
				// 以上已经过滤，此处控制的是如果没有小数点，首位不能为类似于 01、02的金额
				let reg = new RegExp("^(\\d+)\\.(\\d{" + num + "}).*$");
				// val = val.replace(reg, '$1.$2'); //只能输入n个小数
				// console.log(99999, val)
				if (val.indexOf(".") < 0 && val != "") val = parseFloat(val);
				return val
			},
		}
	}
</script>

<style lang="scss" scoped>
</style>
